package com.a214.yxz.yuejian013.Adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.a214.yxz.yuejian013.JavaBean.ShaituBean;
import com.a214.yxz.yuejian013.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Oliver on 10/27/2017.
 */

public class ShaituListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater inflater;
    private List<ShaituBean> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public ShaituListAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.user_post_list_item, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ShaituBean bean = list.get(position);
        if (bean == null) return;
        final ViewHolder viewholder = (ViewHolder) holder;
        if (bean.getShaier() != null) {
            viewholder.user_name.setText(bean.getShaier().getUser_name());
            Picasso.with(context)
                    .load("http://172.24.10.175/workout/Uploads/" + bean.getShaier().getPhoto())
                    .placeholder(R.drawable.user)
                    .into(viewholder.user_pic);
        }
        viewholder.time.setText(bean.getUpdate_time());
        viewholder.title.setText(bean.getTitle());
        viewholder.user_pic.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.user));
        if (mOnItemClickListener != null) {
            viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = viewholder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(viewholder.itemView, position);
                }
            });
            viewholder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = viewholder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(viewholder.itemView, position);
                    return false;
                }
            });
        }
        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/" + bean.getThumb0())
                .placeholder(R.drawable.email)
                .into(viewholder.pic1);
        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/" + bean.getThumb1())
                .placeholder(R.drawable.email)
                .into(viewholder.pic2);
        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/" + bean.getThumb2())
                .placeholder(R.drawable.email)
                .into(viewholder.pic3);
    }

    public void setList(List<ShaituBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    private class ViewHolder extends RecyclerView.ViewHolder {
        TextView user_name, time, title;
        ImageView user_pic, pic1, pic2, pic3;

        public ViewHolder(View itemView) {
            super(itemView);
            user_name = (TextView) itemView.findViewById(R.id.user_post_userName);
            time = (TextView) itemView.findViewById(R.id.user_post_postTime);
            title = (TextView) itemView.findViewById(R.id.user_post_intro);
            user_pic = (ImageView) itemView.findViewById(R.id.user_post_userPic);
            pic1 = (ImageView) itemView.findViewById(R.id.user_post_postPic1);
            pic2 = (ImageView) itemView.findViewById(R.id.user_post_postPic2);
            pic3 = (ImageView) itemView.findViewById(R.id.user_post_postPic3);
        }

    }
}
